package com.school.lab.web.action.admin;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;



import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.school.lab.domain.User;
import com.school.lab.web.WebConstants;


public class AuthorizationInterceptor extends MethodFilterInterceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3381307843264584610L;

	/**
	 * 
	 */
    private String name;

    public String getName()

    {

        return name;

    }

    public void setName(String name)

    {

        this.name = name;

    }

	@Override
	public String doIntercept(ActionInvocation ai) throws Exception {
		Object action = ai.getAction();
		if (action instanceof UserAction) {
			return ai.invoke();
		}
		Map session = ai.getInvocationContext().getSession();
		User user = (User) session.get(WebConstants.SESSION_USER);
		if (null != user) {
			/*
			 * disable the cache of the client
			 * */
			HttpServletResponse response = ServletActionContext.getResponse();     
			response.setHeader("Pragma","No-cache");    
			response.setHeader("Cache-Control","no-cache");    
			response.setDateHeader("Expires", 0);
			return ai.invoke();
		} else {
			return "login";
		}
	}

}
